<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BApplication
 *
 * @author jianfeng
 */
class BApplication {

    //put your code here

    private $currentUser = null;
    private $db = null;
    private $appUi = null;
    private $appPool = null;
    private $activeService = null;
    private $activeApp = null;
    private $activeOpp = null;
    public $basePath = null;
    public $authPath = null;
    public $dataPath = null;
    public $dbPath = null;
    public $configPath = null;   
    public $uiPath = null;
    public $appPath = null;

    public function __construct() {
        $this->initImport();
    }

    private function initImport() {
        //by default, set app basePath to app folder
        if ($this->basePath == NULL)
            $this->basePath = "application.application";

        if ($this->authPath == NULL)
            $this->authPath = $this->basePath . '.auth';

        if ($this->dbPath == NULL)
            $this->dbPath = $this->basePath . '.db';

        if ($this->uiPath == NULL)
            $this->uiPath = $this->basePath . '.ui';

        if ($this->configPath == NULL)
            $this->configPath = $this->basePath . '.config';

        if ($this->dataPath == NULL)
            $this->dataPath = $this->basePath . '.data';

        if ($this->appPath == NULL)
            $this->appPath = $this->basePath . '.app';

        yii::import($this->basePath . '.*');
        yii::import($this->appPath . '.*');
        yii::import($this->uiPath . '.*');
        yii::import($this->authPath . '.*');
        yii::import($this->dataPath . '.*');
        yii::import($this->dbPath . '.*');
    }

    /**
     *
     * @return BUser
     */
    public function getCurrentUser() {
        if ($this->currentUser == null)
            $this->currentUser = new BUser();

        return $this->currentUser;
    }

    /**
     *
     * @return BAppDb
     */
    public function getAppDb() {
        if ($this->db == null)
            $this->db = new BAppDb ();

        return $this->db;
    }

    /**
     *
     * @return BUIApp
     */
    public function getAppUI() {
        if ($this->appUi == null) {
            $this->appUi = new BUIApp ($this);
        }

        return $this->appUi;
    }

    /**
     *
     * @return BAppPool 
     */
    public function getAppPool() {
        if ($this->appPool == NULL)
            $this->appPool = new BAppPool ();

        return $this->appPool;
    }

    /**
     *
     * @return BService
     */
    public function getActiveService() {
        return $this->activeService;
    }

    /**
     *
     * @return BServiceApp
     */
    public function getActiveServiceApp() {
        if ($this->activeApp == NULL)
            $this->activeApp = isset($this->activeService) ? new BServiceEngine($this->activeService) : new BServiceEngine;
        return $this->activeApp;
    }

    /**
     *
     * @return BOperation
     */
    public function getActiveOperation() {
        return $this->activeOpp;
    }

    public function setActiveService($service) {
        $this->activeService = $service;
    }

    public function setActiveServiceApp($app) {
        $this->activeApp = $app;
    }

    public function setActiveOperation($operation) {
        $this->activeOpp = $operation;
    }

}

?>
